<?php


/**
 *  Antz_User is used to control persistent aspects of the user's session
 *  You can put objects into Antz_User, you are encouraged to extend it by
 *  stating: Antz_User extends Your_User , then something like:

class Your_User extends Antz_User
{
public function yourNewMethod...
*/

require_once('Cart.php');

class Antz_User
{
    protected $pageHistory = array();

    public $userid;
    public $username;
    public $timeFormat;
    public $dateFormat;
    public $timeOffset;
    protected $adminUsers;
    public $floodCheck = array();
    public $cart;
    public $useAutoIpLocation = false;
    public $country = 'NZ';
    public $lang = 'en_US';

    public function __construct(){
        $this->adminUsers = array('antz', 'tester');
        $this->userid = 0;
        $this->username = 'Guest';
        $this->timeFormat = 'H:ia';
        $this->dateFormat = 'd m Y';
        $this->longTimeFormat = $this->timeFormat.' '.$this->dateFormat;
        $this->timeOffset = 0;
        $this->cart = Antz::newObject('Antz_Cart');
        $url = 'http://api.hostip.info/country.php?id='.$_SERVER['REMOTE_ADDR'];
        // get auto localization
        //echo 'starting new session';
        if($this->useAutoIpLocation){
          if(function_exists('curl_init')){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, false);
            $this->country = trim(curl_exec($ch));
            curl_close($ch);
            unset($ch);
            
            //echo 'country for '.$_SERVER['REMOTE_ADDR'].' IS '.$this->country.' '.$url;
            
          }else{
            // curl does not exists
            $this->country = trim(@file_get_contents($url));
          }
          
        }
    }

    public function getInstance(){
        global $siteName; // unique value set at earliest point in script execution
        if($_SESSION == NULL){
            // session has not been started
            @session_start();
        };

        if(empty($_SESSION['Antz_User']) || !($_SESSION['Antz_User'] instanceof Antz_User))
            $_SESSION['Antz_User'] = Antz::newObject('Antz_User');
        
        //echo 'new country: '.$_SESSION['Antz_User']->country.' '.$_SERVER['REMOTE_ADDR'];
        
        return $_SESSION['Antz_User'];
    }

    public function isLoggedIn(){
        return ($this->userid > 0);
    }

    public function isAdmin(){
        if(in_array($this->username, $this->adminUsers)) return true;
        else return false;
    }

    public function login($uData){
        $DB = Antz::registry('db');
        $this->userid = $uData['id'];
        $this->username = $uData['username'];
        $this->country = $uData['country'];
        // update login history
        $ips = explode(',', $uData['ip_history']);
        if(!in_array($_SERVER['REMOTE_ADDR'], $ips)) $ips[] = $_SERVER['REMOTE_ADDR'];
        $ips = implode(',', $ips);
        $params = array(
            'last_login_tstamp' => time(),
            'last_login_ip' => $_SERVER['REMOTE_ADDR'],
            'ip_history' => $ips
        );
        $DB->update(cms_db_prefix().'iusers', $params, "id = '{$uData['id']}'");
    }

    public function logout(){
        $this->userid = 0;
        $this->username = 'Guest';
        $_SESSION['lastPage'] = array();
    }

    public function getLang(){
    	return $this->lang;
    }

    public function setLang($lang){
    	$this->lang = $lang;
    }


}// end class
